﻿using HMS.Data;
using HMS.DataContract.Request;
using HMS.DataContract.Response;
using HMS.DataContract.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HMS.Facade
{
    public class ServiceFacade
    {
        public List<DoctorResponse> GetDoctorList(int LocId)
        {
            AppointmentService _appointmentService = new AppointmentService();
            var lstDoctor = _appointmentService.GetDoctorList(LocId);
            List<DoctorResponse> lstDoctorLookup = new List<DoctorResponse>();
            foreach (var item in lstDoctor)
            {
                DoctorResponse lookUp = new DoctorResponse();
                lookUp.Id = item.Doctor.Doc_Id;
                lookUp.Detail = item.Doctor.Doc_Name;
                lstDoctorLookup.Add(lookUp);
            }
            return lstDoctorLookup;
        }

        public List<LocationResponse> GetLocationList(string clinicCode)
        {
            AppointmentService _appointmentService = new AppointmentService();
            var _lstLocation = _appointmentService.GetLocationList(clinicCode);
            List<LocationResponse> lstLocation = new List<LocationResponse>();
            foreach (var item in _lstLocation)
            {
                LocationResponse lookUp = new LocationResponse();
                lookUp.Id = item.Loc_Id;
                lookUp.Detail = item.Loc_Name;
                lstLocation.Add(lookUp);
            }
            return lstLocation;
        }

        public List<DoctorAppointmentResponse> GetAppointmentDetails(int patientId, int DoctorAppointmentId, int DoctorId)
        {
            AppointmentService _appointmentService = new AppointmentService();
            List<DoctorAppointmentResponse> doctorAppointmentResponseList = new List<DoctorAppointmentResponse>();
            List<Doctor_Appointment> appointmentDetailList = _appointmentService.GetAppointmentDetails(patientId, DoctorId);
            List<Doctor_Appointment> sortedAppintments = new List<Doctor_Appointment>();

            if (appointmentDetailList != null)
            {
                var selectedAppt = appointmentDetailList.Where(x => x.Doc_Appt_Id == DoctorAppointmentId).FirstOrDefault();
                sortedAppintments.Add(selectedAppt);
                var appt = appointmentDetailList.Where(x => x.Doc_Appt_Id != DoctorAppointmentId && x.Doc_Appt_Date_Time <selectedAppt.Doc_Appt_Date_Time).OrderByDescending(x => x.Doc_Appt_Date_Time).FirstOrDefault();
                if (appt != null)
                    sortedAppintments.Add(appt);
                foreach (var item in sortedAppintments)
                {
                    DoctorAppointmentResponse doctorAppointmentResponse = new DoctorAppointmentResponse();
                    doctorAppointmentResponse.Id = item.Doc_Appt_Id;
                    doctorAppointmentResponse.DocLocId = item.Doc_Loc_Id;
                    var doctor = _appointmentService.GetDoctorByDocId(item.Doctor_Location.Doc_Id);
                    doctorAppointmentResponse.DoctorEmail = doctor.Doc_Email;
                    doctorAppointmentResponse.DoctorPhone = doctor.Doc_Phone;
                    doctorAppointmentResponse.DoctorName = doctor.Doc_Name;
                    doctorAppointmentResponse.Patient = new PatientResponse()
                    {
                        Id = item.Patient.Patient_id,
                        Name = item.Patient.Patient_Name,
                        Gender = new LookUp() { Id = item.Patient.Patient_Gender },
                        DOB = item.Patient.Patient_DOB,
                        //Clinic
                        Allergies = item.Patient.Patient_Allergies,
                        Phone = item.Patient.Patient_Phone,
                        Email = item.Patient.Patient_Email
                    };
                    doctorAppointmentResponse.AppointmentDateTime = item.Doc_Appt_Date_Time;
                    doctorAppointmentResponse.FollowUpStatus = item.Doc_Appt_Follow_Up;
                    doctorAppointmentResponse.Purpose = item.Doc_Purpose;
                    doctorAppointmentResponse.AppointmentStatus = new LookUp() { Id = item.AppointmentStatu.Appt_Status_Id, Detail = item.AppointmentStatu.Appt_Name };
                    doctorAppointmentResponseList.Add(doctorAppointmentResponse);
                }
            }
            return doctorAppointmentResponseList;
            //return _appointmentService.GetAppointmentDetails(doctorApptId);
        }

        public List<MonthChart> GetMonthlyAppointmentList(int DoctorId, int LocId, int clinicId, DateTime startDate, DateTime endDate)
        {
            AppointmentService _appointmentService = new AppointmentService();
            var _lstAppointment = _appointmentService.GetAppointmentByDocIdLocId(DoctorId, LocId, clinicId, startDate, endDate);
            List<MonthChart> lstMonthChart = new List<MonthChart>();
            var doctor = _appointmentService.GetDoctorByDocId(DoctorId);
            if (_lstAppointment != null && _lstAppointment.Count > 0)
            {
                lstMonthChart = _lstAppointment.GroupBy(x => x.Doc_Appt_Date_Time.Date).Select(y => new MonthChart() { Date = y.Key, Count = y.Count() }).ToList();
                lstMonthChart[0].DoctorEmail = doctor.Doc_Reminder_Email;
                lstMonthChart[0].DoctorNumber = doctor.Doc_Reminder_Phone;
                lstMonthChart[0].DoctorName = doctor.Doc_Name;

            }
            return lstMonthChart;
        }

        public List<DailyList> GetDailyAppointmentList(int DoctorId, int LocId, int clinicId, DateTime startDate, DateTime endDate)
        {
            AppointmentService _appointmentService = new AppointmentService();
            var _lstAppointment = _appointmentService.GetAppointmentByDocIdLocId(DoctorId, LocId, clinicId, startDate, endDate);
            List<DailyList> lstDaily = new List<DailyList>();
            foreach (var item in _lstAppointment)
            {
                DailyList day = new DailyList();
                day.Id = item.Doc_Appt_Id;
                day.PatientId = item.Patient_Id;
                day.PatientName = item.Patient.Patient_Name;
                day.AppointmentTime = item.Doc_Appt_Date_Time;
                day.PatientEmail = item.Patient.Patient_Email;
                day.PatientNumber = item.Patient.Patient_Phone;
                day.DoctorLocId = item.Doc_Loc_Id;
                day.ApptStatusId = item.Doc_Appt_Status_Id;
                day.IsFollowUp = item.Doc_Appt_Follow_Up;
                day.Purpose = item.Doc_Purpose;
                lstDaily.Add(day);
            }
            return lstDaily;
        }

        public DoctorAppointmentResponse SaveAppointmentDetails(DoctorAppointmentResponse doctorAppointmentResponse, string clinicCode)
        {
            AppointmentService _apptService = new AppointmentService();

            var dayOff = _apptService.getDoctorDayOffByDocIdDate(doctorAppointmentResponse.DocId, doctorAppointmentResponse.AppointmentDateTime);
            if (dayOff != null && dayOff.Count > 0)
            {

                doctorAppointmentResponse.ErrorMessage = "Doctor is not available";
                return doctorAppointmentResponse;
                
            }
            else
            {
                if (doctorAppointmentResponse.Id == 0)
                {
                    var docApp = CreateAppointmentDetails(doctorAppointmentResponse, clinicCode);
                    docApp.isNew = true;
                    return docApp;
                }
                else
                    return UpdateAppointmentDetails(doctorAppointmentResponse);
            }
        }

        public ClinicResponse GetClinicName(string clinicCode)
        {
            AppointmentService _appointmentService = new AppointmentService();
            var _clinic = _appointmentService.GetClinicName(clinicCode);
            ClinicResponse clinic = new ClinicResponse();
            clinic.Id = _clinic.Clinic_Id;
            clinic.Detail = _clinic.Clinic_Name;
            clinic.Code = _clinic.Clinic_Code;
            clinic.Address = _clinic.Clinic_Address;
            clinic.Phone = _clinic.Clinic_Phone;
            clinic.LandingPageUrl = _clinic.Clinic_Landing_Page_Url;
            return clinic;
        }

        public DoctorAppointmentResponse DeleteAppointment(int AppointmentId)
        {
            AppointmentService _appointmentService = new AppointmentService();
            var docAppt = _appointmentService.GetAppointmentById(AppointmentId);
            var _doctorLocation = _appointmentService.GetDocLocByDocLocId(docAppt.Doc_Loc_Id);
            var _doctor = _appointmentService.GetDoctorByDocId(_doctorLocation.Doc_Id);
            DoctorAppointmentResponse _docAppt = new DoctorAppointmentResponse();
            _docAppt.AppointmentDateTime = docAppt.Doc_Appt_Date_Time;
            _docAppt._AppointmentDateTime = docAppt.Doc_Appt_Date_Time.ToString("dd/MM/yyyy HH:mm");
            _docAppt.DocLocId = docAppt.Doc_Loc_Id;
            _docAppt.DoctorEmail = _doctor.Doc_Reminder_Email;
            _docAppt.DoctorPhone = _doctor.Doc_Reminder_Phone;
            _docAppt.DoctorName = _doctor.Doc_Name;

            var _patient = _appointmentService.GetPatientByPatientId(docAppt.Patient_Id);
            _docAppt.AccessCode = _patient.Patient_Access_Code;
            _docAppt.Patient = new PatientResponse()
            {
                Id = docAppt.Patient_Id,
                Name = _patient.Patient_Name,
                Email = _patient.Patient_Email,
                Phone = _patient.Patient_Phone
            };
            var isDeleted = _appointmentService.DeleteAppointment(AppointmentId);
            return _docAppt;
        }

        public List<PatientResponse> GetPatientName(string PatientName, int ClinicId)
        {
            List<PatientResponse> lstLookUp = new List<PatientResponse>();
            AppointmentService _appointmentService = new AppointmentService();
            var lstPatient = _appointmentService.GetPatientName(PatientName, ClinicId);
            foreach (var item in lstPatient)
            {
                PatientResponse lookup = new PatientResponse();
                lookup.Id = item.Patient_id;
                lookup.Detail = item.Patient_Name;
                lookup.Phone = item.Patient_Phone;
                lookup.Email = item.Patient_Email;
                lstLookUp.Add(lookup);
            }
            return lstLookUp;
        }

        public DoctorAppointmentResponse GetPatientById(int PatientId,DateTime date)
        {
            AppointmentService _appointmentService = new AppointmentService();
            var _patient = _appointmentService.GetPatientByPatientId(PatientId);
            PatientResponse patient = new PatientResponse();
            DoctorAppointmentResponse docAppointment = new DoctorAppointmentResponse();
            if (_patient != null)
            {
                patient.Id = _patient.Patient_id;
                patient.Name = _patient.Patient_Name;
                patient.Phone = _patient.Patient_Phone;
                patient.Email = _patient.Patient_Email;
                patient.DOB = _patient.Patient_DOB;
                patient.Gender = new LookUp() { Id = _patient.Patient_Gender };
                patient.CreatedBy = _patient.Created_By;
                patient.CreatedDate = _patient.Created_Date;
                patient.ModifiedBy = _patient.Modified_By;
                patient.ModifiedDate = _patient.Modified_Date;
                var _docAppointmentId = _appointmentService.GetPatientAppointmentByPatientId(PatientId, date);

                if (_docAppointmentId != null)
                {
                    docAppointment.Patient = patient;
                    docAppointment.FollowUpStatus = _docAppointmentId.Doc_Appt_Follow_Up;
                    docAppointment.Purpose = _docAppointmentId.Doc_Purpose;
                    docAppointment.AppointmentDateTime = _docAppointmentId.Doc_Appt_Date_Time;
                    docAppointment.AppointmentStatus = new LookUp() { Id = _docAppointmentId.Doc_Appt_Status_Id };
                    docAppointment.DocLocId = _docAppointmentId.Doc_Loc_Id;

                    docAppointment.Id = _docAppointmentId.Doc_Appt_Id;
                    //docAppointment.DocId = _docAppointmentId.d;
                    docAppointment.AppointmentDateTime = _docAppointmentId.Doc_Appt_Date_Time;

                    docAppointment.FollowUpStatus = _docAppointmentId.Doc_Appt_Follow_Up;
                    docAppointment.Purpose = _docAppointmentId.Doc_Purpose; //_masterService.GetPurposeById(doctorAppointmentRequest.PurposeId);
                    docAppointment.AppointmentStatus = new LookUp() { Id = _docAppointmentId.Doc_Appt_Status_Id };
                    docAppointment.CreatedBy = _docAppointmentId.Created_By;
                    docAppointment.CreatedDate = _docAppointmentId.Created_Date;
                    docAppointment.ModifiedBy = _docAppointmentId.Modified_By;
                    docAppointment.ModifiedDate = _docAppointmentId.Modified_Date;
                    docAppointment.AccessCode = _patient.Patient_Access_Code;
                }
                else
                {
                    docAppointment.Patient = patient;

                }
            }
            return docAppointment;
        }

        public AppointmentDocumentResponse SaveImage(AppointmentDocumentRequest appDoc)
        {

            AppointmentService AppService = new AppointmentService();
            Appointment_Document appointment_Document = new Appointment_Document() { Doc_Appt_Id = appDoc.DocAppointmentId, Appt_Image_Name = appDoc.ImageName };
            var appointmentDoc = AppService.SaveImage(appointment_Document);
            AppointmentDocumentResponse appDocResponse = new AppointmentDocumentResponse();
            appDocResponse.Id = appointmentDoc.Doc_Appt_Id.GetValueOrDefault();
            return appDocResponse;
        }

        private DoctorAppointmentResponse CreateAppointmentDetails(DoctorAppointmentResponse doctorAppointmentResponse, string clinicCode)
        {

            AppointmentService _appointmentService = new AppointmentService();
            Doctor_Appointment doctor_Appointment = new Doctor_Appointment();
            MasterSerivce _masterService = new MasterSerivce();
            try
            {
                doctor_Appointment.Doc_Appt_Id = doctorAppointmentResponse.Id;
                Doctor_Location docLoc = _masterService.GetDoctorLocationId(doctorAppointmentResponse.DocId, doctorAppointmentResponse.LocId);
                var location = _masterService.GetLocationByLocId(docLoc.Loc_Id);
                doctor_Appointment.Doc_Loc_Id = docLoc.Doc_Loc_Id;
                doctor_Appointment.Patient = new Patient()
                {
                    Patient_id = doctorAppointmentResponse.Patient.Id,
                    Patient_Name = doctorAppointmentResponse.Patient.Name,
                    Patient_Gender = doctorAppointmentResponse.Patient.Gender.Id,//_masterService.GetGenderById(doctorAppointmentRequest.PatientGenderId),
                    Patient_DOB = doctorAppointmentResponse.Patient.DOB,
                    Patient_Allergies = doctorAppointmentResponse.Patient.Allergies,
                    Patient_Phone = doctorAppointmentResponse.Patient.Phone,
                    Patient_Email = doctorAppointmentResponse.Patient.Email,
                    Patient_Clinic_Id = doctorAppointmentResponse.Patient.Clinic.Id,

                    Created_By = 1,
                    Created_Date = DateTime.Now,
                    Modified_By = 1,
                    Modified_Date = DateTime.Now
                };
                doctor_Appointment.Doc_Appt_Date_Time = doctorAppointmentResponse.AppointmentDateTime;
                doctor_Appointment.Doc_Appt_Follow_Up = doctorAppointmentResponse.FollowUpStatus;
                doctor_Appointment.Doc_Purpose = doctorAppointmentResponse.Purpose; //_masterService.GetPurposeById(doctorAppointmentRequest.PurposeId);
                doctor_Appointment.Doc_Appt_Status_Id = 2;
                doctor_Appointment.Created_By = 1;
                doctor_Appointment.Created_Date = DateTime.Now;
                doctor_Appointment.Modified_By = 1;
                doctor_Appointment.Modified_Date = DateTime.Now;
                //doctor_Appointment.Doc_Reminder_Email = "dummy@dummy.com";
                //doctor_Appointment.Doc_Reminder_Phone = "12345678";


                doctor_Appointment = _appointmentService.SaveAppointmentDetails(doctor_Appointment, clinicCode);

                doctorAppointmentResponse.Id = doctor_Appointment.Doc_Appt_Id;
                doctorAppointmentResponse.DocLocId = doctor_Appointment.Doc_Loc_Id;
                doctorAppointmentResponse.Patient.Id = doctor_Appointment.Patient.Patient_id;
                doctorAppointmentResponse.Patient.Email = doctor_Appointment.Patient.Patient_Email;
                doctorAppointmentResponse.Patient.Phone = doctor_Appointment.Patient.Patient_Phone;
                doctorAppointmentResponse.Patient.Name = doctor_Appointment.Patient.Patient_Name;
                doctorAppointmentResponse.AccessCode = doctor_Appointment.Patient.Patient_Access_Code;
                doctorAppointmentResponse.CreatedBy = doctor_Appointment.Created_By;
                doctorAppointmentResponse.CreatedDate = doctor_Appointment.Created_Date;
                doctorAppointmentResponse.ModifiedBy = doctor_Appointment.Modified_By;
                doctorAppointmentResponse.ModifiedDate = doctor_Appointment.Modified_Date;
                var doctor = _appointmentService.GetDoctorByDocId(doctorAppointmentResponse.DocId);
                doctorAppointmentResponse.DoctorEmail = doctor.Doc_Reminder_Email;
                doctorAppointmentResponse.DoctorPhone = doctor.Doc_Reminder_Phone;
                doctorAppointmentResponse.DoctorName = doctor.Doc_Name;
                //doctorAppointmentResponse.AppointmentDateTime = doctorLoc.Doc_Appt_Date_Time;
                //doctorAppointmentResponse.DocId = doctor_Appointment.Doctor_Location.Doc_Id;
                //doctorAppointmentResponse.LocId = doctor_Appointment.Doctor_Location.Loc_Id;

                return doctorAppointmentResponse;
            }
            catch (Exception ex)
            {
                throw new Exception();
            }
        }

        private DoctorAppointmentResponse UpdateAppointmentDetails(DoctorAppointmentResponse doctorAppointmentResponse)
        {
            AppointmentService _appointmentService = new AppointmentService();

            MasterSerivce _masterService = new MasterSerivce();
            try
            {
                Doctor_Appointment doctor_Appt = new Doctor_Appointment();
                doctor_Appt.Doc_Appt_Id = doctorAppointmentResponse.Id;
                doctor_Appt.Doc_Loc_Id = doctorAppointmentResponse.DocLocId;
                doctor_Appt.Patient = new Patient();
                doctor_Appt.Patient.Patient_id = doctorAppointmentResponse.Patient.Id;
                doctor_Appt.Patient.Patient_Name = doctorAppointmentResponse.Patient.Name;
                doctor_Appt.Patient.Patient_Gender = doctorAppointmentResponse.Patient.Gender.Id; // _masterService.GetGenderById(doctorAppointmentRequest.PatientGenderId);
                doctor_Appt.Patient.Patient_DOB = doctorAppointmentResponse.Patient.DOB;
                var _patient = GetPatientById(doctorAppointmentResponse.Patient.Id, doctorAppointmentResponse.AppointmentDateTime);

                doctor_Appt.Patient.Patient_Allergies = doctorAppointmentResponse.Patient.Allergies;
                doctor_Appt.Patient.Patient_Phone = doctorAppointmentResponse.Patient.Phone;
                doctor_Appt.Patient.Patient_Email = doctorAppointmentResponse.Patient.Email;
                doctor_Appt.Patient.Patient_Clinic_Id = doctorAppointmentResponse.Patient.Clinic.Id;
                doctor_Appt.Patient.Modified_By = 1;
                doctor_Appt.Patient.Modified_Date = DateTime.Now;
                doctor_Appt.Patient.Modified_Date = doctor_Appt.Patient.Modified_Date;
                doctor_Appt.Doc_Appt_Date_Time = doctorAppointmentResponse.AppointmentDateTime;
                doctor_Appt.Doc_Appt_Follow_Up = doctorAppointmentResponse.FollowUpStatus;
                doctor_Appt.Doc_Purpose = doctorAppointmentResponse.Purpose;// _masterService.GetPurposeById(doctorAppointmentRequest.PurposeId);
                doctor_Appt.Doc_Appt_Status_Id = doctorAppointmentResponse.AppointmentStatus.Id;
                //doctor_Appt.AppointmentStatu.Appt_Name = doctorAppointmentResponse.AppointmentStatus.Detail;
                doctor_Appt.Modified_By = 1;
                doctor_Appt.Modified_Date = DateTime.Now;
                doctor_Appt.Modified_Date = doctor_Appt.Modified_Date;

                _appointmentService.UpdateAppointmentDetails(doctor_Appt);

                var doctor = _appointmentService.GetDoctorByDocId(doctorAppointmentResponse.DocId);
                doctorAppointmentResponse.DoctorEmail = doctor.Doc_Reminder_Email;
                doctorAppointmentResponse.DoctorPhone = doctor.Doc_Reminder_Phone;
                doctorAppointmentResponse.DoctorName = doctor.Doc_Name;
                doctorAppointmentResponse.AccessCode = _patient.AccessCode;
                //doctorAppointmentResponse.AppointmentDateTime = doctorLoc.Doc_Appt_Date_Time;
                return doctorAppointmentResponse;
            }
            catch (Exception ex)
            {
                throw new Exception();
            }

        }


        public List<AppointmentDocumentResponse> GetImageList(int DocApptId)
        {
            List<AppointmentDocumentResponse> lstLookUp = new List<AppointmentDocumentResponse>();
            AppointmentService _appointmentService = new AppointmentService();
            var lstPatient = _appointmentService.GetImagesList(DocApptId);
            foreach (var item in lstPatient)
            {
                AppointmentDocumentResponse lookup = new AppointmentDocumentResponse();
                lookup.Id = item.Appt_Document_Id;
                lookup.Detail = item.Appt_Image_Name;
                lookup.Prescription = item.Prescription;
                lookup.CreatedDate = item.Created_Date;
                lstLookUp.Add(lookup);
            }
            return lstLookUp;
        }

        public List<DoctorAppointmentResponse> GetAppointmentHistoryByPatientId(int patientId, int numberOfRecord = 10)
        {
            AppointmentService appSrv = new AppointmentService();
            List<Doctor_Appointment> appointmentHistory = appSrv.GetAppointmentHistoryByPatientId(patientId, numberOfRecord);
            List<DoctorAppointmentResponse> doctorAppointmentList = new List<DoctorAppointmentResponse>();
            foreach (var item in appointmentHistory)
            {
                DoctorAppointmentResponse doctorAppointment = new DoctorAppointmentResponse();
                doctorAppointment.Id = item.Doc_Appt_Id;
                doctorAppointment.DocId = item.Doctor_Location.Doc_Id;
                doctorAppointment.DoctorName = item.Doctor_Location.Doctor.Doc_Name;
                doctorAppointment.LocId = item.Doctor_Location.Loc_Id;
                doctorAppointment.LocName = item.Doctor_Location.Location.Loc_Name;
                doctorAppointment.DocLocId = item.Doc_Loc_Id;
                doctorAppointment.Patient = new PatientResponse()
                {
                    Id = item.Patient.Patient_id,
                    Name = item.Patient.Patient_Name,
                    Gender = new LookUp()
                    {
                        Id = item.Patient.Patient_Gender,
                    },
                    DOB = item.Patient.Patient_DOB,
                    //Clinic
                    Allergies = item.Patient.Patient_Allergies,
                    Phone = item.Patient.Patient_Phone,
                    Email = item.Patient.Patient_Email
                };
                doctorAppointment.AccessCode = item.Patient.Patient_Access_Code;
                doctorAppointment.AppointmentDateTime = item.Doc_Appt_Date_Time;
                doctorAppointment.FollowUpStatus = item.Doc_Appt_Follow_Up;
                doctorAppointment.Purpose = item.Doc_Purpose;
                doctorAppointment.AppointmentStatus = new LookUp() { Id = item.AppointmentStatu.Appt_Status_Id, Detail = item.AppointmentStatu.Appt_Name };
                doctorAppointmentList.Add(doctorAppointment);
            }
            return doctorAppointmentList;
        }
        public int SavePatientDetails(PatientResponse patient)
        {
            PatientService service = new PatientService();
            Patient patientObj = new Patient();
            patientObj.Patient_id = patient.Id;
            patientObj.Patient_Name = patient.Name;
            patientObj.Patient_Gender = patient.Gender.Id;
            patientObj.Patient_Email = patient.Email;
            patientObj.Patient_DOB = patient.DOB;
            patientObj.Patient_Phone = patient.Phone;
            return service.SavePatientDetails(patientObj);
        }

        public AppointmentDocumentResponse SavePatientImage(AppointmentDocumentRequest appDoc)
        {

            AppointmentService AppService = new AppointmentService();
            Appointment_Document appointment_Document = new Appointment_Document() { Patient_Id = appDoc.PatientId, Appt_Image_Name = appDoc.ImageName };
            var appointmentDoc = AppService.SavePatientImage(appointment_Document);
            AppointmentDocumentResponse appDocResponse = new AppointmentDocumentResponse();
            appDocResponse.Id = appointmentDoc.Doc_Appt_Id.GetValueOrDefault();
            return appDocResponse;
        }

        public List<TimeSpan> GetDoctorSlots(int DocId)
        {
            AppointmentService appSrv = new AppointmentService();
            List<Doctor_Slot> doctorSlot = appSrv.GetDoctorSlots(DocId);
            List<TimeSpan> slots = new List<TimeSpan>();
            foreach (var item in doctorSlot)
            {
                slots.Add(item.Slot_From);
                slots.Add(item.Slot_To.GetValueOrDefault());
            }
            return slots;
            //return appSrv.GetDoctorSlots(DocId);
        }

        public List<DateTime> GetDoctorDayOffList(int DocId, DateTime startDate, DateTime endDate)
        {
            AppointmentService appSrv = new AppointmentService();
            Doctor_DayOff docDayOff = new Doctor_DayOff();
            docDayOff.Doc_Id = DocId;
            List<Doctor_DayOff> doctorSlot = appSrv.getDoctorDayOff(docDayOff).Where(x => x.Doc_Day_Off >= startDate && x.Doc_Day_Off <= endDate).ToList<Doctor_DayOff>();
            List<DateTime> dayOff = new List<DateTime>();
            foreach (var item in doctorSlot)
            {
                dayOff.Add(item.Doc_Day_Off);
            }
            return dayOff;
        }

        public DoctorAppointmentResponse GetAppointmentById(int DocApptId)
        {
            AppointmentService _service = new AppointmentService();
            var _doc = _service.GetAppointmentById(DocApptId);
            DoctorAppointmentResponse doc = new DoctorAppointmentResponse();
            if (_doc != null)
            {
                var patient = _service.GetPatientByPatientId(_doc.Patient_Id);
                var docLoc = _service.GetDocLocByDocLocId(_doc.Doc_Loc_Id);
                if (docLoc != null)
                {
                    var doctor = _service.GetDoctorByDocId(docLoc.Doc_Id);
                    if (doctor != null)
                    {
                        doc.DoctorName = doctor.Doc_Name;
                        doc.DoctorPhone = doctor.Doc_Reminder_Phone;
                        doc.DoctorEmail = doctor.Doc_Reminder_Email;
                    }
                }
                doc.Patient = new PatientResponse();
                doc.Patient.Name = patient.Patient_Name;
                doc.Patient.Phone = patient.Patient_Phone;
                doc.Patient.Email = patient.Patient_Email;
                doc.AccessCode = patient.Patient_Access_Code;
                doc.AppointmentDateTime = _doc.Doc_Appt_Date_Time;
            }
            return doc;
        }

        public List<DoctorAppointmentResponse> GetApptbyPatientId(int PatientId)
        {
            AppointmentService _service = new AppointmentService();
            List<DoctorAppointmentResponse> lstDocAppt = new List<DoctorAppointmentResponse>();
            var _appts = _service.GetAppointbyPatientId(PatientId);
            if (_appts != null)
            {
                foreach (var item in _appts)
                {
                    DoctorAppointmentResponse doc = new DoctorAppointmentResponse();
                    var patient = _service.GetPatientByPatientId(item.Patient_Id);
                    var docLoc = _service.GetDocLocByDocLocId(item.Doc_Loc_Id);
                    if (docLoc != null)
                    {
                        var doctor = _service.GetDoctorByDocId(docLoc.Doc_Id);
                        if (doctor != null)
                        {
                            doc.DoctorName = doctor.Doc_Name;
                            doc.DoctorPhone = doctor.Doc_Reminder_Phone;
                            doc.DoctorEmail = doctor.Doc_Reminder_Email;
                        }
                    }
                    doc.Patient = new PatientResponse();
                    doc.Patient.Name = patient.Patient_Name;
                    doc.Patient.Phone = patient.Patient_Phone;
                    doc.Patient.Email = patient.Patient_Email;
                    doc.AppointmentDateTime = item.Doc_Appt_Date_Time;
                    doc.AccessCode = patient.Patient_Access_Code;
                    lstDocAppt.Add(doc);
                }
            }
            return lstDocAppt;
        }

        public bool SavePrescriptionText(AppointmentDocumentRequest appDocRequest)
        {
            AppointmentService _apptService = new AppointmentService();
            Appointment_Document appDoc = new Appointment_Document();
            appDoc.Prescription = appDocRequest.Prescription;
            appDoc.Created_By = 1;
            appDoc.Created_Date = DateTime.UtcNow.AddHours(5).AddMinutes(30);
            appDoc.Modified_By = 1;
            appDoc.Modified_Date = DateTime.UtcNow.AddHours(5).AddMinutes(30);
            appDoc.Patient_Id = null;
            appDoc.Appt_Image_Name = null;
            appDoc.Doc_Appt_Id = appDocRequest.DocAppointmentId;
            return _apptService.SavePrescriptionText(appDoc);
        }

        public bool SaveDoctorDayOff(int DocId, DateTime date)
        {
            AppointmentService _apptService = new AppointmentService();
            Doctor_DayOff docSlot = new Doctor_DayOff();
            docSlot.Created_By = 1;
            docSlot.Created_Date = DateTime.UtcNow.AddHours(5).AddMinutes(30);
            docSlot.Modified_By = 1;
            docSlot.Doc_Day_Off = date;
            docSlot.Modified_Date = DateTime.UtcNow.AddHours(5).AddMinutes(30);
            docSlot.Doc_Id = DocId;
            docSlot.Slot_From = DateTime.Parse("08:00").Subtract(DateTime.Parse(DateTime.MinValue.ToShortTimeString()));
            docSlot.Slot_To = DateTime.Parse("20:00").Subtract(DateTime.Parse(DateTime.MinValue.ToShortTimeString()));
            return _apptService.SaveDoctorDayOff(docSlot);
        }

        public bool DeleteDoctorDayOff(int DocId, DateTime date)
        {
            AppointmentService _apptService = new AppointmentService();
            Doctor_DayOff docSlot = new Doctor_DayOff();
            docSlot.Doc_Day_Off = date;
            docSlot.Doc_Id = DocId;
            return _apptService.DeleteDoctorDayOff(docSlot);
        }
    }
}